authApp.value('page','apply_list'); 

authApp.controller("applyListController",function($scope,$http,page,$chineseNum,Upload, $timeout,$sce,$cookieStore,$cookies){
	$scope.isAddM=true;
	$scope.isUpload=true;
	var requestJson={'pageNumber':'0','pageSize':'10','fid':'0','type':'0'};
	$scope.zpbg=$sce.trustAsHtml("上传电子版");
	$scope.jyfb=$sce.trustAsHtml("上传电子版");
	$scope.zzhb=$sce.trustAsHtml("上传电子版");
	$scope.zyzz=$sce.trustAsHtml("上传电子版");
	$scope.zpbgPath="";
	$scope.jyfbPath="";
	$scope.zzhbPath="";
	$scope.zyzzPath="";
	
	$http({
		method:'post',
		url:apis.queryApplyFileBySchoolId,
		data:{},
	}).success(function(response){
		$scope.zpbg=$sce.trustAsHtml(response.data.selfEvaluationFilename);
		$scope.jyfb=$sce.trustAsHtml(response.data.employmentDistributionFilename);
		$scope.zzhb=$sce.trustAsHtml(response.data.specialtyConstructionFilename);
		$scope.zyzz=$sce.trustAsHtml(response.data.supportingMaterialsFilename);
		$scope.zpbgPath=response.data.selfEvaluationFilepath;
		$scope.jyfbPath=response.data.employmentDistributionFilepath;
		$scope.zzhbPath=response.data.specialtyConstructionFilepath;
		$scope.zyzzPath=response.data.supportingMaterialsFilepath;
		$scope.zpbgN=response.data.selfEvaluationFilename;
		$scope.jyfbN=response.data.employmentDistributionFilename;
		$scope.zzhbN=response.data.specialtyConstructionFilename;
		$scope.zyzzN=response.data.supportingMaterialsFilename;
	})
	
	$scope.getAML=function(){
		$http({
			method:'post',
			url:apis.queryItem,
			data:requestJson,
		}).success(function(response){
			if(response.code=="000000"){
				$scope.list=response.data.data;
				angular.forEach($scope.list,function(data,index,array){
					data.chindexNum=$chineseNum(data.number);
				})
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等1！");
		})
	}
	
	$scope.getAuthInfo=function(){
		$http({
			method:'get',
			url:apis.queryAuthSchoolInfo,
			params:{},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.state=response.data.state;
				if($scope.state=="1"){
					$scope.isUpload=false;
				}
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等2！");
		})
	}
	
	$scope.uploadFiles= function(file, errFiles,index) {
        $scope.f = file;
        $scope.errFile = errFiles && errFiles[0];
        if (file) {
            file.upload = Upload.upload({
                url: apis.uploadTask,
                data: {file: file}
            });

            file.upload.then(function (response) {
                $timeout(function () {
                	if(response.data.code=="000000"){
                		if(index=="0"){
                			$scope.zpbgN=file.name;
                			$scope.zpbg=$sce.trustAsHtml(file.name);
                			$scope.zpbgPath=response.data.data;
                		}
                		if(index=="1"){
                			$scope.jyfbN=file.name;
                			$scope.jyfb=$sce.trustAsHtml(file.name);
                			$scope.jyfbPath=response.data.data;
                		}
                		if(index=="2"){
                			$scope.zzhbN=file.name;
                			$scope.zzhb=$sce.trustAsHtml(file.name);
                			$scope.zzhbPath=response.data.data;
                		}
                		if(index=="3"){
                			$scope.zyzzN=file.name;
                			$scope.zyzz=$sce.trustAsHtml(file.name);
                			$scope.zyzzPath=response.data.data;
                		}
                		file.result = response.data;	
                	}
                	else
                		alert(response.data.errorMsg);
                });
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 * 
                evt.loaded / evt.total));
            });
        }
    }
	
	
		
	
	
	$scope.uploadAll=function(){			
		//if(window.confirm("提交认证申请之前请确认认证申请数据是否填写完成，提交认证申请之后将不能修改！")){
			if($scope.zpbgPath!=""){
				var reqData={
						'selfEvaluationFilepath':$scope.zpbgPath,
						'selfEvaluationFilename':$scope.zpbgN,
						'employmentDistributionFilepath':$scope.jyfbPath,
						'employmentDistributionFilename':$scope.jyfbN,
						'specialtyConstructionFilepath':$scope.zzhbPath,
						'specialtyConstructionFilename':$scope.zzhbN,
						'supportingMaterialsFilepath':$scope.zyzzPath,
						'supportingMaterialsFilename':$scope.zyzzN
					};
				$http({
					method:'post',
					url:apis.submitApplyFile,
					data:reqData,
				}).success(function(response){
					if(response.code=="000000"){
						$http({
							method:'post',
							url:apis.queryNotSubmit,
						}).success(function(response){
							if(response.code=="000000"){
								if(response.data == "ok"){
									if(window.confirm("提交认证申请之前请确认认证申请数据是否填写完成，提交认证申请之后将不能修改！")){
										location.href=apis.onsubmit;
										$http({
											method:'post',
											url:apis.onsubmit,
										}).success(function(response){
											if(response.code=="000000"){
												alert("提交认证申请成功！");
												setTimeout(location.reload(),100); 
											}
										})
									}
								}else{
									alert(response.data);
								}
							}else{
								alert(response.errorMsg);
							}	
						})
					}else{
						alert(response.errorMsg);
					}	
				})
			}else{
				alert("请先上传所有的文档");
			}
		//}		
	}
	
	
	$scope.turnTo=function(pid,number){
		location.href= "apply_auth.html?pid="+pid+"&state="+$scope.state+"&number="+number;
	}
	
	$scope.getAuthInfo();
	$scope.getAML();
})